package dao.daoImpl;

import common.DataSourceUtils;
import dao.PermissionMapper;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import pojo.Function;
import pojo.SecoundMenu;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 2017-12-10.
 */
public class PermissionMapperImpl implements PermissionMapper {

    @Override
    public List<Function> manageModule() throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from function";
        return qr.query(sql,new BeanListHandler<Function>(Function.class));
    }

    @Override
    public List<Function> getCurrFunction(int pageNow, int page_size) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from function order by moduleName limit ?,?";
        return qr.query(sql,new BeanListHandler<Function>(Function.class),(pageNow-1)*page_size,page_size);
    }

    @Override
    public int openFunction(int id,String functionDoc) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="update function set functionDoc=? where id=?";
        return qr.update(sql,functionDoc,id);
    }

    @Override
    public int delFunction(int id) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="delete from function where id=?";
        return qr.update(sql,id);
    }

    @Override
    public List<SecoundMenu> getSecoundMenu() throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from secoundMenu";
        return qr.query(sql,new BeanListHandler<SecoundMenu>(SecoundMenu.class));
    }

    @Override
    public Function checkFunction(String moduleName, String secoundMenu) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from function where moduleName=? and functionName=?";
        return qr.query(sql,new BeanHandler<Function>(Function.class),moduleName,secoundMenu);
    }

    @Override
    public SecoundMenu getFid(String secoundMenu) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from secoundMenu where secoundMenu=?";
        return qr.query(sql,new BeanHandler<SecoundMenu>(SecoundMenu.class),secoundMenu);
    }

    @Override
    public int addSecoundMenu(SecoundMenu secoundMenu) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="insert into secoundmenu (secoundMenu,url,fid) values(?,?,?)";
        return qr.update(sql,secoundMenu.getSecoundMenu(),secoundMenu.getUrl(),secoundMenu.getFid());
    }

    @Override
    public int addFunction(Function function) throws SQLException {
        String sql="insert into function (functionId,moduleId,function,functionName,moduleName,functionAddress) values(?,?,?,?,?,?)";
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        int result=qr.update(sql,function.getFunctionId(),function.getModuleId(),function.getFunction(),function.getFunctionName(),function.getModuleName(),function.getFunctionAddress());
        return result;
    }

}
